﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Interface;
using Lamex.Library.CommonClass;
using System.IO;
using DevExpress.Utils.Menu;

namespace Lamex.mQuality.UI
{
    public partial class FrmQC_SeatScanRecord : Lamex.Library.frmBaseChild
    {
        DataTable dt = null;
        public FrmQC_SeatScanRecord()
        {
            InitializeComponent();
            this.Load += new EventHandler(FrmQC_SeatScanRecord_Load);
            txtDateStart.UcProperties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            txtDateStart.UcProperties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            txtDateStart.UcProperties.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            txtDateStart.UcProperties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;

            txtDateEnd.UcProperties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            txtDateEnd.UcProperties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            txtDateEnd.UcProperties.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            txtDateEnd.UcProperties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;

            txtDateStart.UcEditValue = DateTime.Now.ToString("yyyy-MM-dd")+" 00:00:00";
            txtDateEnd.UcEditValue = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
        }
        private void FrmQC_SeatScanRecord_Load(object sender,EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            DataTable dtDistinc = Lamex.Base.BaseBuildSql.GetDataTableBySql(@"select distinct(BARCODE_TYPE) as BARCODE_TYPE from HR_Scan_Record where ISNULL(BARCODE_TYPE,'')!=''");
            foreach(DataRow dr in dtDistinc.Rows)
            {
                txtStationType.Properties.Items.Add(dr["BARCODE_TYPE"]);
            }
        }

        public override void DoView(IButtonInfo sender)
        {
            string sql = @"select a.BARCODE,b.BARCODE_DESC,a.SCAN_QTY,a.TIME_LENGTH,a.CANCEL_FLAG,a.SCAN_TIME,a.SCANNER_NO,a.LOCATION_CODE,a.STATION_NAME,a.BARCODE_TYPE from [dbo].[HR_Scan_Record] a
                           left join[dbo].[HR_Work_Barcode] b on b.BARCODE_SN=a.BARCODE where 1=1 ";
            string strWhere = "";
            if(!string.IsNullOrEmpty(txtBarCode.Text))
            {
                strWhere += " and a.BARCODE='"+txtBarCode.Text+"'";
            }
            if (!string.IsNullOrEmpty(txtStationName.Text))
            {
                strWhere += " and a.STATION_NAME='" + txtStationName.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtLocation.Text))
            {
                strWhere += " and a.LOCATION_CODE='" + txtLocation.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtStationType.Text))
            {
                strWhere += " and a.BARCODE_TYPE='" + txtStationType.Text + "'";
            }

            if (!string.IsNullOrEmpty(txtDateStart.Text))
            {
                strWhere += " and a.SCAN_TIME>='" + txtDateStart.Text + "'";
            }

            if (!string.IsNullOrEmpty(txtDateEnd.Text))
            {
                strWhere += " and a.SCAN_TIME<='" + txtDateEnd.Text + "'";
            }
            sql = sql + strWhere+ " order by a.SCAN_TIME  ";
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            View.DataSource = dt;
            base.DoView(sender);
        }

        public override void DoExport(IButtonInfo sender)
        {
            string path= @"D:\ReportFile"; ;
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (txtStationType.Text=="QC")
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.InitialDirectory= path;
                saveFile.FileName = @"QCFile";
                saveFile.Filter = "Excel files(*.xls)|*.xls";
                if (DialogResult.OK== saveFile.ShowDialog())
                {
                    string fileName = saveFile.FileName;
                    FileImoprtHelper.ImorpQCTypeList(dt, fileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("导出成功", "提示", MessageBoxButtons.OK);
                }
                
            }
            else if(txtStationType.Text == "EQ")
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.InitialDirectory = path;
                saveFile.FileName = @"EQFile";
                saveFile.Filter = "Excel files(*.xls)|*.xls";
                if (DialogResult.OK == saveFile.ShowDialog())
                {
                    string fileName = saveFile.FileName;
                    FileImoprtHelper.ImorpQCTypeList(dt, fileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("导出成功", "提示", MessageBoxButtons.OK);
                }
            }
            else
            {
                SaveFileDialog dlg = new SaveFileDialog();
                //dlg.Filter = "PDF file(*.pdf)|*.pdf";
                dlg.Filter = "Excel文件(*.xls)|*.xls";
                dlg.FileName = @"ALLFile";
                if (dlg.ShowDialog() == DialogResult.OK)
                {


                    //view.ExportToPdf(dlg.FileName);
                    Lamex.Common.ExecelHelper.DataTableToExcel(dt, dlg.FileName);
                    //View.ExportToXls(dlg.FileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("导出成功", "提示", MessageBoxButtons.OK);
                }
            }

            base.DoExport(sender);
        }
    }
}